# _*_ coding: utf-8 _*_
# .@FileName:expData
# .@Data....:2025-04-03 : 14 : 20
# .@Aurhor..:LiuJingYu
"""
launch:
from __future__ import unicode_literals, print_function
import maya.cmds as mc
"""
import importlib
import maya.cmds as cmds
importlib.reload(cmds)
import json


def export_controllers_to_json(file_path):
    # 获取当前选择的控制器和曲线
    selected_objects = cmds.ls(selection=True)

    if not selected_objects:
        print("未选择任何对象，请先选择要导出的控制器或曲线。")
        return

    object_data = {}

    for obj in selected_objects:
        if cmds.objectType(obj) == 'nurbsCurve':
            # 获取曲线的 CV 点位置
            cv_points = cmds.getAttr(f'{obj}.cv[*]')
            object_data[obj] = {
                "type": "nurbsCurve",
                "cv_points": cv_points
            }
        else:
            # 获取变换信息
            translation = cmds.xform(obj, query=True, translation=True, worldSpace=True)
            rotation = cmds.xform(obj, query=True, rotation=True, worldSpace=True)
            scale = cmds.xform(obj, query=True, scale=True, worldSpace=True)

            # 获取所有可设置的属性
            attributes = cmds.listAttr(obj, settable=True)
            attr_values = {}
            if attributes:
                for attr in attributes:
                    try:
                        value = cmds.getAttr(f"{obj}.{attr}")
                        attr_values[attr] = value
                    except:
                        continue

            object_data[obj] = {
                "type": "controller",
                "translation": translation,
                "rotation": rotation,
                "scale": scale,
                "attributes": attr_values
            }

    # 将数据保存到 JSON 文件
    try:
        with open(file_path, 'w') as f:
            json.dump(object_data, f, indent=4)
        print(f"对象数据已成功导出到 {file_path}")
    except Exception as e:
        print(f"导出数据时出错: {e}")


def import_controllers_from_json(file_path):
    try:
        with open(file_path, 'r') as f:
            object_data = json.load(f)

        for obj, data in object_data.items():
            if data["type"] == "nurbsCurve":
                if not cmds.objExists(obj):
                    # 创建新的曲线
                    new_curve = cmds.curve(point=data["cv_points"], name=obj)
                else:
                    new_curve = obj
                    # 设置曲线的 CV 点位置
                    for i, point in enumerate(data["cv_points"]):
                        cmds.xform(f'{new_curve}.cv[{i}]', translation=point, worldSpace=True)
            else:
                if not cmds.objExists(obj):
                    # 创建新的控制器（这里以创建 nurbsCircle 为例）
                    new_controller = cmds.createNode('nurbsCurve',name=obj)[0]
                else:
                    new_controller = obj

                # 设置变换信息
                cmds.xform(new_controller, translation=data["translation"], worldSpace=True)
                cmds.xform(new_controller, rotation=data["rotation"], worldSpace=True)
                cmds.xform(new_controller, scale=data["scale"], worldSpace=True)

                # 设置属性值
                for attr, value in data["attributes"].items():
                    try:
                        cmds.setAttr(f"{new_controller}.{attr}", value)
                    except:
                        print(f"设置 {new_controller}.{attr} 属性值时出错。")

        print(f"对象数据已成功从 {file_path} 导入。")
    except Exception as e:
        print(f"导入数据时出错: {e}")


file_path = r"F:/Python/zbrig-class/Data/BaseSetLoc.json"
import_controllers_from_json(file_path)








